dining is package{
  import semaphore;
  
  -- Dining philosophers as actors and semaphores
  
  table(Count) do let{
      T is semaphore(3);
      
      phil(n,L,R) is spawn{
        for Ix in iota(1,Count,1) do{
          -- sleep(random(15L));
          T.grab();  -- get permission first
          L.grab();
          R.grab();
          -- logMsg(info,"Phil $n is eating for the $(Ix)th time");
          T.release();
          -- sleep(random(10L));
          L.release();
          R.release();
        }
      logMsg(info,"Phil $n ate for $(Count) times");
      };
    } in {
      fork1 is semaphore(1);
      fork2 is semaphore(1);
      fork3 is semaphore(1);
      fork4 is semaphore(1);
    
      phil1 is phil(1,fork1,fork2);
      phil2 is phil(2,fork2,fork3);
      phil3 is phil(3,fork3,fork4);
      phil4 is phil(4,fork4,fork1);
    
      waitfor phil1;
      waitfor phil2;
      waitfor phil3;
      waitfor phil4;
    };
    
  main() do {
    table(50000);
  }
}
  
    